
package com.yunfei.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {

    @Bean(value = "defaultApi")
    public Docket defaultApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupApiV2Info())
                .groupName("2.云合同后台接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunfei.api.v1"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo groupApiV2Info(){
        return new ApiInfoBuilder()
                .title("财富管理平台App端云合同中心接口文档")
                .description("财富管理平台App端云合同中心接口文档")
                .termsOfServiceUrl("http://10.0.100.200/")
                .contact("zhengyunfei@ylcaifu.com")
                .version("2.0")
                .build();
    }
    @Bean(value = "qianDuanApi")
    public Docket qianDuanApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(groupQianDuanApInfo())
                .groupName("1.云合同前端接口文档")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yunfei.api.v2"))
                .paths(PathSelectors.any())
                .build().securityContexts(Lists.newArrayList(securityContext(),securityContext1())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey(),apiKey1()));

    }


    private ApiInfo groupQianDuanApInfo(){
        return new ApiInfoBuilder()
                .title("财富管理平台App端云合同中心接口文档")
                .description("云合同前端接口文档")
                .termsOfServiceUrl("http://10.0.100.200/")
                .contact("zhengyunfei@ylcaifu.com")
                .version("2.0")
                .build();
    }

    private ApiKey apiKey() {
        return new ApiKey("BearerToken", "Authorization", "header");
    }
    private ApiKey apiKey1() {
        return new ApiKey("BearerToken1", "Authorization-x", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder()
               // .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }
    private SecurityContext securityContext1() {
        return SecurityContext.builder()
              //  .securityReferences(defaultAuth1())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }
}
